<?php
define('ROOT', '../..');
require ROOT . '/lib/includeForBlog.php';

if (false) {
	fetchConfigVal();
}

# Locale language file support, defualt is en-us.php
if (!isset($blog['blogLanguage'])) {
	$blog['blogLanguage'] = $service['language'];
}

if(empty($pluginPath)) $pluginPath = ROOT.'/plugins/HC_signuptoy';
if(isset($__ts)) unset($__ts);
$__ts = array();
// Locale language file support.
switch($blog['blogLanguage']) {
	case "zh-TW":
	case "zh-CN":
		$languageFile = $pluginPath.'/lang/'.$blog['blogLanguage'].'.php';
	break;
	default:
		$languageFile = "";
}
if(!empty($languageFile) && file_exists($languageFile)) { include ($languageFile); }
if (file_exists($pluginPath.'/lang/pluginLocal.php')) require_once ($pluginPath.'/lang/pluginLocal.php');

$existsuserid = array();
$existsusername = array();
$rows = DBQuery::queryAll("select loginid,name from {$database['prefix']}Users");
foreach( $rows as $row ) {
	$existsuserid[] = $row["loginid"];
	$existsusername[] = $row["name"];
}
$rows = DBQuery::queryAll("select loginid from {$database['prefix']}UsersignupLogs");
foreach( $rows as $row ) {
	$existsuserid[] = $row["loginid"];
}
$loginid = POD::escapeString(urldecode($_POST['loginid']));
$name = POD::escapeString(urldecode($_POST['username']));

if(in_array($loginid, $existsuserid)) {
	$message = '<img src="'.ROOT.'/plugins/HC_signuptoy/images/action_cancel.png" border="0" width="48" height="48" alt="" /><br /><br />'.pluginLang::plts('登入帳號已存在！');
	$error = -2;
} elseif (in_array($name, $existsusername)) {
	$message = '<img src="'.ROOT.'/plugins/HC_signuptoy/images/action_cancel.png" border="0" width="48" height="48" alt="" /><br /><br />'.pluginLang::plts('使用者名稱已存在！');
	$error = -2;
} elseif (isset($_POST) && !empty($loginid) && !empty($name)) {
	$password = md5(urldecode($_POST['passwd']));
	$signuptype = 'teamblog';
	$ip = POD::escapeString($_SERVER['REMOTE_ADDR']);
	$host = POD::escapeString(urldecode($_POST['teamblog']));
	POD::query("INSERT INTO {$database['prefix']}UsersignupLogs values($blogid, '$loginid', '$password', '$name', UNIX_TIMESTAMP(), '$signuptype','$ip','$host')");
	
	$message = '<img src="'.ROOT.'/plugins/HC_signuptoy/images/action_ok.png" border="0" width="48" height="48" alt="" /><br /><br />'.pluginLang::plts('登錄成功！');
	$error = 0;
} else {
	$message = "No Request data.";
	$error = -2;
}
$message = '<div style="text-align:center;">'.$message.'</div>';
respond::PrintResult(array('error' => $error , 'result' => $message));
?>